package com.ias.agdis_r.manager;

import android.text.TextUtils;

import com.ias.agdis_r.terminalmanager.restful.findluggage.vo.FindLuggageInfoResponseVO;
import com.ias.agdis_r.terminalmanager.restful.findluggage.vo.LuggageMessageVO;
import com.ias.agdis_r.util.date.PrettyDataFormaterUtil;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * 翻行李信息数据缓存
 * Created by zhaowl on 2018/6/13.
 */
public class FindLuggageDataManager {

    private static FindLuggageDataManager instance;
    private List<FindLuggageInfoResponseVO> listFindLuggageInfo;	// 翻行李信息
    private Set<String> findLuggageTnbSet= new TreeSet<String>();   //翻行李信息初始化列表

    public static FindLuggageDataManager getInstance() {
        if (instance == null) {
            instance = new FindLuggageDataManager();
        }
        return instance;
    }

    private FindLuggageDataManager() { }

    public void delFindLuggageTnb(String tnb){

        findLuggageTnbSet.remove(tnb);
    }

    public void saveFindLuggageTnb(String tnb){
        findLuggageTnbSet.add(tnb);
    }

    public  Set<String>  getFindLuggageTnbSet() {
        return findLuggageTnbSet;
    }

    /**
     * 根据tnb获取对应的翻行李信息
     * @param tnb
     * @return
     */
    public FindLuggageInfoResponseVO getListFindLuggageInfo(String tnb) {
        List<FindLuggageInfoResponseVO> listFindLuggageInfo = getListFindLuggageInfo();
        if (listFindLuggageInfo == null) {
            return null;
        }

        for (FindLuggageInfoResponseVO findLuggageInfo: listFindLuggageInfo) {
            if (findLuggageInfo.getTaskId().equals(tnb)) {
                return findLuggageInfo;
            }
        }
        return null;
    }

    /**
     * 根据任务号和消息id获取任务对应翻行李消息
     * @param tnb
     * @param messageId
     * @return
     */
    public LuggageMessageVO getLuggageMessageByMsgId(String tnb, String messageId) {
        if (TextUtils.isEmpty(tnb) || TextUtils.isEmpty(messageId)) {
            return null;
        }
        return getLuggageMessageByMsgId(getListFindLuggageInfo(tnb), messageId);
    }

    /**
     * 根据翻行李消息Id获取翻行李信息下指定的翻行李消息
     * @param luggageInfoResponseVO 翻行李信息
     * @param messageId 翻行李消息Id
     * @return  翻行李消息
     */
    public LuggageMessageVO getLuggageMessageByMsgId(FindLuggageInfoResponseVO luggageInfoResponseVO, String messageId) {
        if (TextUtils.isEmpty(messageId) || luggageInfoResponseVO == null) {
            return null;
        }
        List<LuggageMessageVO> luggageMessageVoList = luggageInfoResponseVO.getLuggageMessageVoList();
        if (luggageMessageVoList == null || luggageMessageVoList.isEmpty()) {
            return null;
        }
        for (LuggageMessageVO luggageMessageVO: luggageMessageVoList) {
            if (luggageMessageVO.getMessageId().equals(messageId)) {
                return luggageMessageVO;
            }
        }
        return null;
    }

    /**
     * 添加一个翻行李信息
     * @param luggageInfoResponseVO 翻行李信息
     */
    public void addListFindLuggageInfo(FindLuggageInfoResponseVO luggageInfoResponseVO) {
        if (luggageInfoResponseVO == null
                || luggageInfoResponseVO.getLuggageMessageVoList() == null
                || luggageInfoResponseVO.getLuggageMessageVoList().isEmpty())
            return;
        if (getListFindLuggageInfo() == null) {
            setListFindLuggageInfo(new ArrayList<FindLuggageInfoResponseVO>());
        }

        FindLuggageInfoResponseVO findLuggageInfo = getListFindLuggageInfo(luggageInfoResponseVO.getTaskId());
        // 本地添加该任务的翻行李消息
        if (findLuggageInfo == null) {
            getListFindLuggageInfo().add(luggageInfoResponseVO);
            return;
        }

        // 本地对应任务翻行李信息下，增加翻行李消息
        List<LuggageMessageVO> listLuggageMessage = findLuggageInfo.getLuggageMessageVoList();
        if (listLuggageMessage == null) {
            listLuggageMessage = new ArrayList<>(luggageInfoResponseVO.getLuggageMessageVoList());
            return;
        }

        // 去重(利用HashSet防止重复消息)，时间倒序
        // Set<LuggageMessageVO> luggageMessageSet = new HashSet<>();
        Set<LuggageMessageVO> luggageMessageSet = new TreeSet<>(new Comparator<LuggageMessageVO>() {
            @Override
            public int compare(LuggageMessageVO vo1, LuggageMessageVO vo2) {
                if (vo1.getMessageId().equals(vo2.getMessageId()))
                    return 0;
                String format = "yyyy-MM-dd hh:mm:ss";
                long time1 = PrettyDataFormaterUtil.formatString2Time(vo1.getReceptionTime(), format);
                long time2 = PrettyDataFormaterUtil.formatString2Time(vo2.getReceptionTime(), format);
                return time1 < time2 ? 1 : -1;
            }
        });
        luggageMessageSet.addAll(luggageInfoResponseVO.getLuggageMessageVoList());
        luggageMessageSet.addAll(listLuggageMessage);
        listLuggageMessage.clear();
        listLuggageMessage.addAll(luggageMessageSet);
    }

    public List<FindLuggageInfoResponseVO> getListFindLuggageInfo() {
        return listFindLuggageInfo;
    }

    public void setListFindLuggageInfo(List<FindLuggageInfoResponseVO> listFindLuggageInfo) {
        this.listFindLuggageInfo = listFindLuggageInfo;
    }


}
